package business;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;

import htsjdk.samtools.util.CloseableIterator;
import htsjdk.variant.variantcontext.Genotype;
import htsjdk.variant.variantcontext.VariantContext;
import htsjdk.variant.vcf.VCFFileReader;

public class VCFReader {
	private VCFFileReader reader;
	//private HashMap<String,HashSet<VariantContext>> vc;
	
	public VCFReader (String filepath, String indexpath)
	{
		reader = new VCFFileReader(new File(filepath), new File(indexpath));
		//vc = new HashMap<String,HashSet<VariantContext>>();
	}
	
	/*public void prep ()
	{
		CloseableIterator<VariantContext> c = reader.iterator();
		while(c.hasNext())
		{
			VariantContext v = c.next();
			String chrom = v.getChr();
			if(vc.get(chrom) == null)
			{
				HashSet<VariantContext> hs = new HashSet<VariantContext>();
				hs.add(v);
				vc.put(chrom, hs);
			}
			else {
				vc.get(chrom).add(v);
			}
		}
	}*/
	
	public HashSet<VariantContext> query (String chromosome, int start, int end, String transcript_id) //query the vcfData through the library
	{
		CloseableIterator<VariantContext> c = reader.query(chromosome, start, end); //returns all VC objects within the range
		HashSet<VariantContext> results = new HashSet<VariantContext>();
		while(c.hasNext())
		{
			results.add(c.next());
		}
		return results;
	}
	
	/*public HashSet<VariantContext> query1 (String chromosome, int start, int end, String transcript_id)
	{
		HashSet<VariantContext> results = new HashSet<VariantContext>();
		HashSet<VariantContext> search = vc.get(chromosome);
		for(VariantContext v: search)
		{
			if(v.getEnd() >= start && v.getEnd() <= end)
			{
				results.add(v);
			}
		}
		return results;
	}*/
	
	public String getHeaders () //unused
	{
		return reader.getFileHeader().toString();
	}
	
	public static void main (String[] args) {
		VCFReader v = new VCFReader("C:\\Users\\Lucas\\Desktop\\demo\\demo\\GTEx_5M_plus_exome_185_Dec2012.phasing.noimputation.vcf.gz", "C:\\Users\\Lucas\\Desktop\\demo\\demo\\GTEx_5M_plus_exome_185_Dec2012.phasing.noimputation.vcf.gz.tbi");
		//v.prep();
		HashSet<VariantContext> a = v.query("11", 1916652, 1916654, null);
		for(VariantContext c: a)
		{
			/*
			for(Genotype g: c.getGenotypes())
			{
				System.out.println(g.getSampleName());
			}
			*/
			System.out.println(c.getReference().toString().substring(0,1));
			System.out.println(c.getAlternateAllele(0).toString());
		}
	}
	
}
